﻿--[[
   @brief    descprtion: badge_model
   @author   hanmanhuang
   @version  1.0
   @date     2016-12-27
--]]

badge_model = {

    badge_detail_page_url = LoadStr("BADGE_DETAIL_PAGE_URL");
    badge_points_page_url = LoadStr("BADGE_POINT_PAGE_URL");
    badge_levelup_page_url = LoadStr("BADGE_LEVELUP_PAGE_URL");

    badge_jump_main_type = "badge";
    badge_jump_detail_type = "BADGE_DETAIL";
    badge_jump_points_type = "BADGE_POINTS";
    badge_jump_levelup_type = "BADGE_LEVELUP";

    con_badge_oss_cfg = nil;

    uin = 0;
    tgp_id = 0;

    first_rev_level_info = true;
    judge_level = 0;    -- 当前鉴赏家等级
    judge_level_next_need = 0; -- 下一个等级还需要的经验

    init = function()

    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[badge_model] %s", msg), core_msg);
    end;
    
    -- »ñÈ¡»ÕÕÂÏêÇéÒ³url
    get_badge_detail_page_url = function(info)
        local owner = "0";
        if info.owner ~= nil then
            owner = tostring(info.owner);
        end;
        local guest = "0";
        if info.guest ~= nil then
            guest = tostring(info.guest);
        end;
        local badge_type = "0";
        if info.badge_type ~= nil then
            badge_type = tostring(info.badge_type);
        end;
        local show_wall = "0";
        if info.show_wall ~= nil then
            show_wall = tostring(info.show_wall);
        end;
        local url = badge_model.badge_detail_page_url.."?owner="..owner.."&guest="..guest
                    .."&badge_type="..badge_type.."&show_wall="..show_wall;
        return url;
    end;
    
    --»ñÈ¡»ÕÕÂ»ý·ÖÒ³url
    get_badge_points_page_url = function(info)
        local owner = "0";
        if info.owner ~= nil then
            owner = tostring(info.owner);
        end;
        local url = badge_model.badge_points_page_url.."?owner="..owner;
        return url;
    end;
    
    -- »ñÈ¡»ÕÕÂÉý¼¶Ò³url
    get_badge_levelup_page_url = function(info)
        local badge_level = "0";
        if info.badge_level ~= nil then
            badge_level = tostring(info.badge_level);
        end;
        local badge_level_next_need = "0";
        if info.badge_level_next_need ~= nil then
            badge_level_next_need = tostring(info.badge_level_next_need);
        end
        local url = badge_model.badge_levelup_page_url.."?badge_level="..badge_level
                    .."&badge_level_next_need="..badge_level_next_need;
        return url;
    end;
    
    -- »ñÈ¡¼øÉÍ¼Ò»ÕÕÂµÈ¼¶¶ÔÓ¦µÄÃû³Æ
    get_con_badge_name = function (level)
        if nil == badge_model.con_badge_oss_cfg then
            return;
        end;

        local level_name = nil;
        for k, v in pairs(badge_model.con_badge_oss_cfg) do
            if tonumber(v.badge_type) == level then
                level_name = v.badge_name;
                break;
            end;
        end;
        
        return level_name;
    end;
    
    -- ¼øÉÍ¼Ò»ÕÕÂ×î´óµÈ¼¶
    get_max_con_badge_level = function ()
        if nil == badge_model.con_badge_oss_cfg then
            return;
        end;
        
        local max_level = 0;
        for k, v in pairs(badge_model.con_badge_oss_cfg) do
            if tonumber(v.badge_type) > max_level then
                max_level = tonumber(v.badge_type);
            end;
        end;
        return max_level;
    end;

    get_user_base_info = function()
        local base_info = {};
        if badge_model.uin == 0 or badge_model.tgp_id == 0 then
            local user_game_info = ierd_tgp.users_info.User_game_info:new();
            local users_info_adapter =
                base_utility.get_component_adapter("IUsers_info_module",
                    "ierd_tgp.users_info.GetUsersInfoModuleAdapter();");
            if nil ~= users_info_adapter and nil ~= user_game_info then
                users_info_adapter:get_self_game_info(user_game_info);
                badge_model.uin = user_game_info.QQid;
                badge_model.tgp_id = user_game_info.ullTGPID;
            end
            user_game_info:delete();
        end;
        base_info.uin = badge_model.uin;
        base_info.tgp_id = badge_model.tgp_id;
        return base_info;
    end;

    set_info = function(event_id, json_str)
        -- 在此处对model内数据进行设置
        if json_str == nil or json_str == "" then
            local user_game_info = ierd_tgp.users_info.User_game_info:new_local();
            local users_info_adapter =
                base_utility.get_component_adapter("IUsers_info_module",
                    "ierd_tgp.users_info.GetUsersInfoModuleAdapter();");
            users_info_adapter:get_self_game_info(user_game_info);
            badge_model.judge_level = user_game_info.unJudgeLevel;
            badge_model.judge_level_next_need = user_game_info.unJudgeLevelNextNeed;
        else
            if type(json_str) == "number" then
                return;
            end;
            local user_game_info = base_utility.json_to_table(json_str);
            if user_game_info ~= nil and type(user_game_info) ~= "number" then
                local last_judge_level = badge_model.judge_level;

                badge_model.judge_level = user_game_info.JudgeLevel;
                badge_model.judge_level_next_need = user_game_info.JudgeLevelNextNeed;

                base_utility.log(string.format("[badge info] last_level=%d, cur_level=%d",
                    last_judge_level, badge_model.judge_level));

                -- 等级上升了
                if false == badge_model.first_rev_level_info and
                   badge_model.judge_level > last_judge_level then
                    -- badge_control.handle_judge_tips(badge_model.judge_level,
                    --     badge_model.judge_level_next_need);
                end

                if badge_model.first_rev_level_info then
                    badge_model.first_rev_level_info = false;
                end
            end;
        end;
    end;
};

badge_model.init();
